<?php

namespace app\controller;

use app\BaseController;
use app\service\WarehouseService;
use app\service\WmsProviderService;
use think\exception\ValidateException;
use think\facade\Request;

/**
 * 供应商
 */
class WmsProvider extends BaseController
{
    /**
     * @desc 仓库类型 业务类型
     * @return void
     */
    public function info()
    {
        $info = WarehouseService::info();
        returnSuccess(200, '获取成功', $info);
    }

    //货主或者客户列表
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['contract_no'] = Request::param('contract_no/s', '');
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = WmsProviderService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //货主或者客户列表
    public function wms_lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = WmsProviderService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //新增wms 货主或者客户
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        //货物类型 1工业品 2 加工食品  3 汽车零配件 4 海鲜 5 电器
        $param['contract_no'] = getDay() . GetRandInt(6);
        $param['enterprise_type'] = Request::param('enterprise_type/s', '');
        $param['link_mobile'] = Request::param('link_mobile/s', '');
        $param['charge_person'] = Request::param('charge_person/s', '');
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['address'] = Request::param('address/s', '');
        $param['cooperation_province_id'] = Request::param('cooperation_province_id/d', 0);
        $param['cooperation_city_id'] = Request::param('cooperation_city_id/d', 0);
        $param['cooperation_district_id'] = Request::param('cooperation_district_id/d', 0);
        //attr_ids 仓库属性
        $param['cooperation_sdate'] = Request::param('cooperation_sdate/s', '');
        $param['cooperation_edate'] = Request::param('cooperation_edate/s', '');
        $param['cate_ids'] = Request::param('cate_ids/s', '');
        $param['brand_ids'] = Request::param('brand_ids/s', '');
        $param['status'] = Request::param('status/d', 1);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\WmsCustomer::class)->check([
                'name' => $param['name'],
                'enterprise_type' => $param['enterprise_type'],
                'charge_person' => $param['charge_person'],
                'link_mobile' => $param['link_mobile'],
                'cate_ids' => $param['cate_ids'],
                'brand_ids' => $param['brand_ids'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = WmsProviderService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改货主或者客户
    public function modify()
    {
        $id = Request::param('id/d', 0);
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        //货物类型 1工业品 2 加工食品  3 汽车零配件 4 海鲜 5 电器
        $param['enterprise_type'] = Request::param('enterprise_type/s', '');
        $param['link_mobile'] = Request::param('link_mobile/s', '');
        $param['charge_person'] = Request::param('charge_person/s', '');
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['address'] = Request::param('address/s', '');
        $param['cooperation_province_id'] = Request::param('cooperation_province_id/d', 0);
        $param['cooperation_city_id'] = Request::param('cooperation_city_id/d', 0);
        $param['cooperation_district_id'] = Request::param('cooperation_district_id/d', 0);
        //attr_ids 仓库属性
        $param['cooperation_sdate'] = Request::param('cooperation_sdate/s', '');
        $param['cooperation_edate'] = Request::param('cooperation_edate/s', '');
        $param['cate_ids'] = Request::param('cate_ids/s', '');
        $param['brand_ids'] = Request::param('brand_ids/s', '');
        $param['status'] = Request::param('status/d', 1);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\WmsCustomer::class)->check([
                'name' => $param['name'],
                'enterprise_type' => $param['enterprise_type'],
                'charge_person' => $param['charge_person'],
                'link_mobile' => $param['link_mobile'],
                'cate_ids' => $param['cate_ids'],
                'brand_ids' => $param['brand_ids'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = WmsProviderService::edit($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除货主或者客户
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $logParam['ip'] = Request::ip();
        $data = WmsProviderService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '删除成功');
        } else {
            returnFail(21101, '删除失败');
        }
    }
}
